package xy.web.startStop;

import org.apache.log4j.Logger;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import xy.web.util.FileUtil;


public final class StarStopLoader {
	private StarStopLoader(){
		
	}
	private static StarStopLoader instance = new StarStopLoader();
	
	public static void load(String xml) throws Exception{
		instance.loadList(xml);
	}
	private Logger log = Logger.getLogger(StarStopLoader.class);

	private void loadList(String xml) throws Exception {
		
		Element root = FileUtil.getXmlRoot(xml);

		NodeList items = root.getChildNodes();

		int size = items.getLength();
		for (int i = 0; i < size; i++) {
			Node item = items.item(i);	//action
			if(!item.getNodeName().equals("item")){
				continue;
			}
			String clsPath = item.getTextContent().trim();
			if(clsPath.trim().length()==0){
				continue;
			}
			
			try{
				Class<?> cls = Class.forName(clsPath);
				
				IStartStopItem load = (IStartStopItem)cls.newInstance();
				load.process();
				
				log.info("load and process Start Stop: "+clsPath);
			}catch(Exception e){
				log.error("failed to load and process start or stop", e);
			}
		}
	}
}
